package com.gzxw.mogublog.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzxw.mogublog.commons.entity.SubjectItem;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.xo.service.SubjectItemService;
import com.gzxw.mogublog.xo.vo.SubjectItemVO;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/subjectItem")
public class SubjectItemController {

    @Autowired
    private SubjectItemService subjectItemService;


    @PostMapping("/getList")
    public R getList(@RequestBody SubjectItemVO subjectItemVO) {

        IPage<SubjectItem> pageList = subjectItemService.getList(subjectItemVO);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", pageList.getRecords());
        map.put("total", pageList.getTotal());
        map.put("size", pageList.getSize());
        map.put("current", pageList.getCurrent());

        return R.success("返回成功", map);

    }

    @PostMapping("/add")
    public R add(@RequestBody List<SubjectItemVO> subjectItemVOList) {

        String message = subjectItemService.addSubjectItem(subjectItemVOList);

        return R.success("200", message);

    }

    // 编辑专题Item
    @PostMapping("/edit")
    public R edit(@RequestBody List<SubjectItemVO> subjectItemVOList) {

        String message = subjectItemService.editSubjectItem(subjectItemVOList);

        return R.success("200", message);

    }

    //批量删除专题
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<SubjectItemVO> subjectItemVOList) {

        String message = subjectItemService.deleteBatch(subjectItemVOList);

        return R.success("200", message);
    }


    @PostMapping("/sortByCreateTime")
    public R sortByCreateTime(@RequestParam(name = "subjectUid", required = true) String subjectUid,
                              @RequestParam(name = "isDesc", required = false, defaultValue = "false") Boolean isDesc) {

       String message = subjectItemService.sortByCreateTime(subjectUid,isDesc);

        return R.success("200",message);
    }
}